package com.jicheng.service.impl;

import com.jicheng.Utils.DESEncryptUtil;
import com.jicheng.Utils.PicUtil;
import com.jicheng.Utils.ServerUtil;
import com.jicheng.dto.data.IntegrityListParams;
import com.jicheng.dto.data.profile.CadreInformationInfo;
import com.jicheng.dto.integrity.*;
import com.jicheng.dto.task.PagingTemplateParams;
import com.jicheng.dto.task.TemplateDto;
import com.jicheng.dto.task.TemplateFileDto;
import com.jicheng.entity.*;
import com.jicheng.mapper.*;
import com.jicheng.service.IInfoQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: InfoQueryServiceImpl
 * @Description: TODO
 * @author: khz
 * @date: 2020年08月20日 15:39
 */
@Service
public class InfoQueryServiceImpl implements IInfoQueryService {
    @Autowired
    BusinessSituationMapper businessSituationMapper;
    @Autowired
    CadreInformationMapper cadreInformationMapper;
    @Autowired
    FamilyMainMemberMapper familyMainMemberMapper;
    @Autowired
    HouseSituationMapper houseSituationMapper;
    @Autowired
    ParttimeSituationMapper parttimeSituationMapper;
    @Autowired
    SituationMapper situationMapper;
    @Autowired
    InfoQueryMapper infoQueryMapper;
    @Autowired
    TemplateLibraryMapper templateLibraryMapper;
    @Override
    public  List<IntegrityListDto> getIntegrityList(IntegrityListParams integrityListParams) {
        return cadreInformationMapper.getIntegrityList(integrityListParams);
    }

    @Override
    public IntergrityDetailDto getIntegritydetail(String reportId, String cadreId, String appId) {

        List<BusinessSituationDto> businessSituation = businessSituationMapper.getBusinessSituation(reportId, cadreId, appId);
        CadreInformationDto cadreInformation = cadreInformationMapper.getCadreInformation(reportId, cadreId, appId);
        //把头像转成图片
        if(cadreInformation.getHeader() !=null && cadreInformation.getHeader()!=""){
            try {
                String base64 = DESEncryptUtil.decryption("H:/key.k", cadreInformation.getHeader().replace("\\", "/"), "decrypt");
                cadreInformation.setHeader("data:image/jpg;base64,"+base64);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        List<FamilyMainMemberDto> familyMainMember = familyMainMemberMapper.getFamilyMainMember(reportId, cadreId, appId);
        List<HouseSituationDto> houseSituation = houseSituationMapper.getHouseSituation(reportId, cadreId, appId);
        Collections.reverse(houseSituation);
        List<ParttimeSituationDto> parttimeSituation = parttimeSituationMapper.getParttimeSituation(reportId, cadreId, appId);
        Collections.reverse(parttimeSituation);
        SituationDto situation = situationMapper.getSituation(reportId, cadreId, appId);
        IntergrityDetailDto detailDto = new IntergrityDetailDto();
        detailDto.setBusinessSituationInfo(businessSituation);
        detailDto.setCadreInformationInfo(cadreInformation);
        detailDto.setFamilyMainMemberInfo(familyMainMember);
        detailDto.setHouseSituationInfo(houseSituation);
        detailDto.setParttimeSituationInfo(parttimeSituation);
        detailDto.setSituationInfo(situation);
        return detailDto;
    }

    @Override
    public List<Map> getAppUser() {
        return infoQueryMapper.getAppUser();
    }

    @Override
    public List<String> getUnit() {
        return infoQueryMapper.getUnit();
    }

    @Override
    public List<String> getPeriod() {
        return infoQueryMapper.getPeriod();
    }

    @Override
    public List<TemplateDto> getTempLibList(PagingTemplateParams params) {
        return templateLibraryMapper.getTempLibList(params);
    }

    @Override
    public TemplateFileDto getTempLibDetail(String id, HttpServletRequest request) {
        TemplateFileDto templateDetail = templateLibraryMapper.getTempLibDetail(id);
        if (templateDetail.getFile() != null) {
            try {
                String base64 = DESEncryptUtil.decryption("H:/key.k", templateDetail.getFile().replace("\\", "/"), "decrypt");
                templateDetail.setFile("data:image/jpg;base64,"+base64);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return templateDetail;

    }


}
